package com.nd.mobile.study.view;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Gallery;
import android.widget.ListAdapter;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.domain.News;
import com.nd.mobile.study.domain.NewsCatalogRoot;
import com.nd.mobile.study.domain.User;
import com.nd.mobile.study.view.adapter.HomeExpandListAdapter;
import com.nd.mobile.study.view.adapter.HomeGalleryAdapter;
import com.nd.mobile.study.view.widget.FlowIndicator;

public class HomeActivity extends BaseActivity {
	
	static final int SCROLL_ACTION = 0;
	ExpandableListView mExpandableListView;
	Gallery mGallery;
	HomeGalleryAdapter mGalleryAdapter;
	FlowIndicator mMyView;
	Timer mTimer;
	private HomeExpandListAdapter	mAdapter;
	private Intent mIntent2Detail;
	
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.home);		
	}
	
	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.app_name);
	}
	
	@Override
	protected void initViews() {
		this.initHeader();
		mHeaderBtnBack.setVisibility(View.GONE);
		
		mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
		View header = LayoutInflater.from(this).inflate(R.layout.home_header_view,
				null);
		mGallery = (Gallery) header.findViewById(R.id.home_gallery);
		mMyView = (FlowIndicator) header.findViewById(R.id.myView);
		mGalleryAdapter = new HomeGalleryAdapter(this);
		mMyView.setCount(mGalleryAdapter.getCount());
		mGallery.setAdapter(mGalleryAdapter);
		mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				mMyView.setSeletion(arg2);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		mExpandableListView.addHeaderView(header);
		
		mAdapter = new HomeExpandListAdapter(this);
		mExpandableListView.setAdapter(mAdapter);
		mExpandableListView
				.setOnGroupExpandListener(new OnGroupExpandListener() {

					@Override
					public void onGroupExpand(int arg0) {
						mAdapter.tags[arg0] = 1;
					}
				});
		mExpandableListView
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {

					@Override
					public void onGroupCollapse(int arg0) {
						mAdapter.tags[arg0] = 0;
					}
				});
		mIntent2Detail = new Intent().setClass(this, NewsDetailActivity.class);
		mExpandableListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				ExpandableListAdapter adapter = mExpandableListView.getExpandableListAdapter();
				News[] news = ((HomeExpandListAdapter)adapter).getData();
				final News currNews = news[childPosition];
				mIntent2Detail.putExtra(BundleConstants.NEWS, currNews);
				startActivity(mIntent2Detail);
				return null != currNews;
			}
		});
	}
	
	private class GetLatestNewsTask extends SimpleAsyncTask<Void, Void, News[]>{

		public GetLatestNewsTask(Context context) {
			super(context);
		}

		@Override
		protected News[] onLoad(Void... params) throws Exception {
			return NewsCatalogRoot.getLatestNews();
		}

		@Override
		protected void onUI(News[] result) {
			mAdapter.setData(result);
			mAdapter.notifyDataSetChanged();
			mExpandableListView.expandGroup(0);
		}
	}
	
	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		mTimer = new Timer();
		mTimer.scheduleAtFixedRate(new MyTask(), 0, 5000);
		
		autoLogin();
		loadLatestNews();
	}
	
	
	private void loadLatestNews() {
		mLoading.p();
		new GetLatestNewsTask(this).execute();
	}

	private void autoLogin(){
		String [] userInfos = User.getUserInfoFromSharedPreferences(mApp);
	}
	
	private class MyTask extends TimerTask {
		@Override
		public void run() {
			mHandler.sendEmptyMessage(SCROLL_ACTION);
		}
	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case SCROLL_ACTION:
				// int curPos = mGallery.getSelectedItemPosition();
				// if (curPos < mGalleryAdapter.getCount() - 1) {
				// curPos++;
				// } else {
				// curPos = 0;
				// }
				// // mGallery.setLayoutAnimation(new LayoutAnimationController(
				// // AnimationUtils.loadAnimation(HomeActivity.this,
				// // R.anim.gallery_in)));
				// mGallery.setSelection(curPos, true);
				MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
						SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
						89.333336f, 265.33334f, 0);
				MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
						SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
						300.0f, 238.00003f, 0);

				mGallery.onFling(e1, e2, -1300, 0);
				break;

			default:
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = (keyCode == KeyEvent.KEYCODE_BACK);
        if (flag) {
            showExitDialog();
        }
        return flag;
	}
}